package swu.cx.master_of_mental_arithmetic

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.SavedStateViewModelFactory
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import kotlinx.android.synthetic.main.fragment_question.*
import swu.cx.master_of_mental_arithmetic.databinding.FragmentQuestionBinding


class QuestionFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //return inflater.inflate(R.layout.fragment_question, container, false)
        val viewModel = ViewModelProvider(requireActivity(),SavedStateViewModelFactory(requireActivity().application,requireActivity())).get(MyViewModel::class.java)
        viewModel.generator()
        viewModel.getCurrentScore()?.value = 0
        val binding = DataBindingUtil.inflate<FragmentQuestionBinding>(inflater,R.layout.fragment_question,container,false)
        binding.data = viewModel
        binding.lifecycleOwner = requireActivity()
        val stringBuilder =StringBuilder()
        val listener = View.OnClickListener {
            when(it.id){
                R.id.button0->stringBuilder.append("0")
                R.id.button1->stringBuilder.append("1")
                R.id.button2->stringBuilder.append("2")
                R.id.button3->stringBuilder.append("3")
                R.id.button4->stringBuilder.append("4")
                R.id.button5->stringBuilder.append("5")
                R.id.button6->stringBuilder.append("6")
                R.id.button7->stringBuilder.append("7")
                R.id.button8->stringBuilder.append("8")
                R.id.button9->stringBuilder.append("9")
            }
            if (stringBuilder.isEmpty()){
                binding.textView13.text = getString(R.string.input_indicator)
            }else{
                binding.textView13.text = stringBuilder.toString()
            }
        }
       binding.button0.setOnClickListener(listener)
       binding.button1.setOnClickListener(listener)
       binding.button2.setOnClickListener(listener)
       binding.button3.setOnClickListener(listener)
       binding.button4.setOnClickListener(listener)
       binding.button5.setOnClickListener(listener)
       binding.button6.setOnClickListener(listener)
       binding.button7.setOnClickListener(listener)
       binding.button8.setOnClickListener(listener)
       binding.button9.setOnClickListener(listener)
       binding.buttonclear.setOnClickListener {
           stringBuilder.clear()
           binding.textView13.text = getString(R.string.input_indicator)
       }
        binding.buttonsubmit.setOnClickListener {
            if (stringBuilder.isEmpty()){
                stringBuilder.append("-1")
            }
            if (stringBuilder.toString().toInt()== viewModel.getAnswer()?.value!!){
                viewModel.answerCorrect()
                stringBuilder.clear()
                binding.textView13.text = getString(R.string.answerCorrect)
            }else{
                if (viewModel.win_flag){
                  val action = QuestionFragmentDirections.actionQuestionFragmentToWinFragment()
                    findNavController().navigate(action)
                    viewModel.win_flag = false
                    viewModel.save()
                }else{
                    val action = QuestionFragmentDirections.actionQuestionFragmentToLoseFragment()
                    findNavController().navigate(action)
                }
            }
        }
        return binding.root
    }

}